package com.question.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.question.pojo.entity.CommentLike;
import com.question.pojo.vo.CommentLikeVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentLikeMapper extends BaseMapper<CommentLike> {

    /**
     * 查询是否点赞
     * @param commentLike
     * @return
     */
    @Select("select * from comment_likes where comment_id = #{commentId} and user_id = #{userId}")
    Boolean selectByCommentIdAndUserId(CommentLike commentLike);

    /**
     * 删除用户评论点赞
     * @param commentLike
     */
    @Delete("delete from comment_likes where comment_id = #{commentId} and user_id = #{userId}")
    void deleteByUserIdAndQuestionId(CommentLike commentLike);

    /**
     * 获取评论点赞数
     * @param questionId
     * @return
     */
    @Select("select qc.comment_id, count(cl.comment_id) as like_count from question_comments qc " +
            "left join comment_likes cl on qc.comment_id = cl.comment_id " +
            "where qc.question_id = #{questionId} " +
            "group by qc.comment_id")
    List<CommentLikeVO> getCommentLikeCount(Integer questionId);

    /**
     * 获取用户点赞的评论列表
     * @param questionId
     * @param userId
     * @return
     */
    @Select("select cl.comment_id from question_comments c " +
            "left join comment_likes cl on c.comment_id = cl.comment_id and cl.user_id = #{param2} " +
            "where c.question_id = #{param1} and cl.comment_id is not null")
    List<Integer> getCommentLikeListByUserId(Integer questionId, Integer userId);

    /**
     * 批量删除评论点赞
     * @param commentId
     */
    @Delete("delete from comment_likes where comment_id = #{commentId}")
    void deleteByCommentId(Integer commentId);
}
